package com.mars.push.core.channel.apns;

import cn.teaey.apns4j.Apns4j;
import cn.teaey.apns4j.network.ApnsChannel;
import cn.teaey.apns4j.network.ApnsChannelFactory;
import cn.teaey.apns4j.protocol.ApnsPayload;
import com.mars.push.core.channel.apns.config.IosPushConfig;
import com.mars.push.core.channel.apns.dto.ApnsPushMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author King
 * @create 2020/5/25 13:41
 */
@Slf4j
@Component
public class ApnsPushServer {

    private static final int TRY_TIMES = 3;

    @Autowired
    private IosPushConfig config;


    /**
     * 使用 Apns4j 方式推送
     *
     * @param message
     */
    public void push(ApnsPushMessage message) {

        //todo 要先去创建生成keystore的证书和证书密码 然后在这边build
        ApnsChannelFactory apnsChannelFactory = Apns4j.newChannelFactoryBuilder().build();

        ApnsChannel apnsChannel = apnsChannelFactory.newChannel(TRY_TIMES);

        //todo 可以根据消息定义要发送的业务消息到apple服务器
        ApnsPayload apnsPayload = Apns4j.newPayload()
                .alertBody(message.getAlert())
                .sound("default");
        //apnsChannel.send(message.get, apnsPayload);

        apnsChannel.close();
    }

}
